package jl.cts.objects;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import jl.cts.R;
import jl.cts.util.Sprite;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class Street {

	private int x;
	private int y;
	private int width;
	private int height;
	private int numRoads = 8;
	private ArrayList<Road> roads;

	private Player rabbit;

	private Sprite sideWalk;

	private Timer blockTimer;

	private int[] colors = { Color.parseColor("#add5e4"),
			Color.parseColor("#ace9ff"), Color.parseColor("#add5e4"),
			Color.parseColor("#ace9ff"), Color.parseColor("#add5e4"),
			Color.parseColor("#ace9ff"), Color.parseColor("#add5e4"),
			Color.parseColor("#ace9ff") };

	private int[] difficulty = { 1, 2, 3, 4, 4, 3, 2, 1 };
	private Timer playerCollisionTimer;
	private boolean blocked = false;

	public Street(Context context, int x, int y, int width, int height) {
		this.x = x;
		this.y = y;
		this.height = height;
		this.width = width;

		init(context);

	}

	private void init(Context context) {

		sideWalk = new Sprite(context, R.drawable.ic_launcher, x, y
				+ (int) (height * 0.9));

		rabbit = new Player(context, x + width / 2, sideWalk.getY());
		roads = new ArrayList<Road>();

		int roadHeigth = (int) (height * 0.9) / numRoads;

		for (int i = 0; i < numRoads; i++) {

			int speedX = numRoads - Math.abs((numRoads / 2) - i);
			speedX = i % 2 == 0 ? -1 * speedX : 1 * speedX;
			speedX = speedX / 2;

			Road road = new Road(context, difficulty[i], x, y + i * roadHeigth,
					x + width, roadHeigth, colors[i], speedX);

			roads.add(road);
		}

		playerCollisionTimer = new Timer();
		playerCollisionTimer.schedule(new TimerTask() {
			@Override
			public void run() {
				if (rabbit.CollidePlayer(roads)) {
					moveRabbitDown(2);
					blockInPut(500);
				}
			}
		}, 0, 500);

		
	}

	public void render(Canvas canvas, Paint paint) {

		if (sideWalk != null) {
			sideWalk.render(canvas, paint);
		}

		for (Road road : roads) {
			road.render(canvas, paint);
		}

		if (rabbit != null) {
			rabbit.render(canvas, paint);
		}

	}

	public void moveRabbitUp(int speed) {
		if (!blocked) {
			speed = Math.abs(speed);
			rabbit.setSpeed(0, -speed);
			if (rabbit.isOutOfTheStreetUp(y)) {
				rabbit.setMoving(false);
			} else {
				rabbit.setMoving(true);
			}
		}

	}

	public void moveRabbitDown(int speed) {
		if (!blocked) {
			speed = Math.abs(speed);
			rabbit.setSpeed(0, speed);
			if (rabbit.isOutOfTheStreetDown(y, height)) {
				rabbit.setMoving(false);
			} else {
				rabbit.setMoving(true);
			}
		}

	}

	public void setAcceleration(float acelX, float acelY, float acelZ) {

		if (acelX > 1) {
			moveRabbitDown(5);
		} else if (acelX < -1) {
			moveRabbitUp(5);
		} else {
			rabbit.setMoving(false);
		}

	}

	public void blockInPut(int time) {

		if (blockTimer != null) {
			blockTimer.cancel();
			blockTimer.purge();
		}

		blocked = true;

		blockTimer = new Timer();

		blockTimer.schedule(new TimerTask() {

			@Override
			public void run() {
				blocked = false;
			}
		}, time);
	}
}